package com.ssyt.examinationsystem.controller;

import com.alibaba.fastjson.JSON;
import com.ssyt.examinationsystem.entity.BlankInfo;
import com.ssyt.examinationsystem.entity.CourseInfo;
import com.ssyt.examinationsystem.entity.PageBean;
import com.ssyt.examinationsystem.service.IBlankInfoService;
import com.ssyt.examinationsystem.service.impl.BlankInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Servlet implementation class BlankInfoController
 */
public class BlankInfoController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public BlankInfoController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 调用业务对象
        IBlankInfoService blankService = new BlankInfoServiceImpl();

        PrintWriter out = response.getWriter();
        // 获取地址栏上的flag值
        String flag = request.getParameter("flag");
        if ("query".equals(flag)) {
            query(request, response);
        } else if ("blank".equals(flag)) {
            blank(blankService, request, response);
        } else if ("list".equals(flag)) {
            list(blankService, request, response, out);
        } else if ("add".equals(flag)) {
            add(blankService, request, response, out);
        } else if ("dele".equals(flag)) {
            delete(blankService, request, response, out);
        } else if ("update".equals(flag)) {
            update(blankService, request, response, out);
        }

        out.flush();
        out.close();
    }

    /**
     * 修改填空题
     *
     * @param blankService
     * @param request
     * @param response
     * @param out
     */
    private void update(IBlankInfoService blankService, HttpServletRequest request, HttpServletResponse response,
                        PrintWriter out) {
        // 获取表单数据
        String question = request.getParameter("question");
        String answer = request.getParameter("answer");
        String courseId = request.getParameter("courseId");
        String blankId = request.getParameter("blankId");
        // 创建一个填空题对象，并封装属性
        BlankInfo blank = new BlankInfo();
        blank.setQuestion(question);
        blank.setAnswer(answer);
        CourseInfo course = new CourseInfo();
        course.setCourseId(Integer.valueOf("".equals(courseId) ? "0" : courseId));
        blank.setCourse(course);
        blank.setBlankId(Integer.valueOf("".equals(blankId) ? "0" : blankId));
        // 调用添加的业务方法
        int row = blankService.updateBlank(blank);
        if (row > 0) {
            out.print("ok");
        }
    }

    /**
     * 删除所有填空题
     *
     * @param blankService
     * @param request
     * @param response
     * @param out
     */
    private void delete(IBlankInfoService blankService, HttpServletRequest request, HttpServletResponse response,
                        PrintWriter out) {
        String blankId = request.getParameter("blankId");
        if (blankService.deleteBlank(blankId) > 0) {
            out.print("ok");
        }
    }

    /**
     * 添加填空题的方法
     *
     * @param blankService
     * @param request
     * @param response
     * @param out
     */
    private void add(IBlankInfoService blankService, HttpServletRequest request, HttpServletResponse response,
                     PrintWriter out) {
        // 获取表单数据
        String question = request.getParameter("question");
        String answer = request.getParameter("answer");
        String courseId = request.getParameter("courseId");
        // 创建一个填空题对象，并封装属性
        BlankInfo blank = new BlankInfo();
        blank.setQuestion(question);
        blank.setAnswer(answer);
        CourseInfo course = new CourseInfo();
        course.setCourseId(Integer.valueOf("".equals(courseId) ? "0" : courseId));
        blank.setCourse(course);
        // 调用添加的业务方法
        int row = blankService.addBlankInfo(blank);
        if (row > 0) {
            out.print("ok");
        }
    }

    /**
     * 分页查询所有填空题
     *
     * @param blankService
     * @param request
     * @param response
     * @param out
     */
    private void list(IBlankInfoService blankService, HttpServletRequest request, HttpServletResponse response,
                      PrintWriter out) {
        PageBean page = new PageBean();
        // 获取当前页
        String current = request.getParameter("page");
        // 获取每页显示数
        String pageSize = request.getParameter("rows");
        // 获取页面传入当前页
        if (current != null) {
            Integer currentPage = Integer.valueOf(current);
            page.setCurrentPage(currentPage);
            page.setPageSize(Integer.valueOf(pageSize));
        }

        PageBean pageBean = blankService.getBlankByPage(page);

        Map<String, Object> jsonData = new HashMap<String, Object>();
        jsonData.put("total", pageBean.getTotalRecords());
        jsonData.put("rows", pageBean.getList());
        String jsonDataString = JSON.toJSONString(jsonData, true);

        out.print(jsonDataString);
    }

    /**
     * 显示所有填空题到选题窗口
     *
     * @param blankService
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void blank(IBlankInfoService blankService, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<Map<String, Object>> blanks = blankService.getBlank();
        request.setAttribute("blanks", blanks);
        // 转发到选题页面
        request.getRequestDispatcher("WEB-INF/views/group/selectQuestion.jsp").forward(request, response);

    }

    private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 转发到填空题显示页面
        request.getRequestDispatcher("WEB-INF/views/question/blank.jsp").forward(request, response);

    }

}
